﻿package mortal.game.view.common
{
    import Message.Public.*;
    import com.mui.controls.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.mvc.core.*;

    public class DoubleCountDownBox extends GSprite
    {
        public var countDownUrl:String;
        public var startUrl:String;
        public var endUrl:String;
        private var _titleBitmap:GImageBitmap;
        private var _doubleCountDown:SecTimerBitmapView;
        private var _bg:GImageBitmap;
        protected var _timeOutCallBack:Function;
        private var _sClientActive:SClientActive;

        public function DoubleCountDownBox()
        {
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._bg = UIFactory.gImageBitmap(ImagesConst.doubleCountDowmBg, 0, 0, this);
            this._doubleCountDown = UIFactory.secTimerBitmapView(110, 20, "DoubleRewardTimerNum.png", 18.18, 27, 0, null, 10);
            this._doubleCountDown.customCharIndex = {0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9, ::10};
            this._doubleCountDown.setParse("hh:mm:ss");
            this._doubleCountDown.timeOutHandler = this.timeOutCallBack;
            this._doubleCountDown.caller = this.updateTime;
            this._titleBitmap = UIFactory.gImageBitmap("", 28, 20, this, this.resetPos);
            Dispatcher.addEventListener(EventName.ActiveStart, this.updateActive);
            Dispatcher.addEventListener(EventName.ActiveEnd, this.updateActive);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._titleBitmap.dispose(param1);
            this._titleBitmap = null;
            this._doubleCountDown.dispose(param1);
            this._doubleCountDown = null;
            this._bg.dispose(param1);
            this._bg = null;
            this._timeOutCallBack = null;
            this._sClientActive = null;
            this.countDownUrl = "";
            this.startUrl = "";
            this.endUrl = "";
            Dispatcher.removeEventListener(EventName.ActiveStart, this.updateActive);
            Dispatcher.removeEventListener(EventName.ActiveEnd, this.updateActive);
            super.disposeImpl(param1);
            return;
        }// end function

        private function resetPos() : void
        {
            if (this.isDisposed)
            {
                return;
            }
            if (this._doubleCountDown)
            {
                this._doubleCountDown.x = this._titleBitmap.x + this._titleBitmap.width + 5;
                this.addChild(this._doubleCountDown);
            }
            return;
        }// end function

        private function updateActive(event:DataEvent) : void
        {
            var _loc_2:* = event.data as SClientActive;
            if (this._sClientActive && this._sClientActive.type == _loc_2.type)
            {
                this.updateTime();
            }
            return;
        }// end function

        protected function updateTime() : void
        {
            var _loc_1:Number = NaN;
            var _loc_2:Number = NaN;
            var _loc_3:Number = NaN;
            if (this._sClientActive)
            {
                _loc_1 = ClockManager.instance.nowDate.time;
                if (this._sClientActive.startDt && this._sClientActive.endDt)
                {
                    _loc_2 = this._sClientActive.startDt.time;
                    _loc_3 = this._sClientActive.endDt.time;
                    if (_loc_1 < _loc_2)
                    {
                        this.titleUrl = this.countDownUrl;
                        this.retsetTitlePos(28, 20);
                        this._doubleCountDown.x = this._titleBitmap.x + this._titleBitmap.width + 5;
                        this._doubleCountDown.visible = true;
                    }
                    else if (_loc_1 >= _loc_3)
                    {
                        this.titleUrl = this.endUrl;
                        this.retsetTitlePos(0, 0);
                        this._doubleCountDown.stop();
                        this._doubleCountDown.visible = false;
                    }
                    else
                    {
                        this.titleUrl = this.startUrl;
                        this.retsetTitlePos(0, 0);
                        this._doubleCountDown.stop();
                        this._doubleCountDown.visible = false;
                    }
                }
            }
            else
            {
                this.titleUrl = this.endUrl;
                this.retsetTitlePos(0, 0);
                this._doubleCountDown.stop();
                this._doubleCountDown.visible = false;
            }
            return;
        }// end function

        public function get sClientActive() : SClientActive
        {
            return this._sClientActive;
        }// end function

        public function set sClientActive(param1:SClientActive) : void
        {
            var _loc_2:int = 0;
            var _loc_3:Number = NaN;
            this._sClientActive = param1;
            if (this._sClientActive)
            {
                _loc_3 = ClockManager.instance.nowDate.time;
                if (_loc_3 < this._sClientActive.startDt.time)
                {
                    _loc_2 = Math.ceil((this._sClientActive.startDt.time - _loc_3) * 0.001) + 1;
                }
                else if (_loc_3 > this._sClientActive.endDt.time)
                {
                    _loc_2 = 0;
                }
                else
                {
                    _loc_2 = 0;
                }
                this.setLeftTime(_loc_2);
                this.updateTime();
                this.visible = true;
            }
            else
            {
                this._doubleCountDown.stop();
                this.visible = false;
            }
            return;
        }// end function

        private function timeOutCallBack() : void
        {
            if (this._timeOutCallBack != null)
            {
                this._timeOutCallBack.apply();
            }
            return;
        }// end function

        public function setParse(param1:String) : void
        {
            this._doubleCountDown.setParse(param1);
            return;
        }// end function

        public function setLeftTime(param1:int) : void
        {
            this._doubleCountDown.setLeftTime(param1);
            return;
        }// end function

        public function set timeOutHandler(param1:Function) : void
        {
            this._timeOutCallBack = param1;
            return;
        }// end function

        public function set bgUrl(param1:String) : void
        {
            this._bg.imgUrl = param1;
            return;
        }// end function

        public function set titleUrl(param1:String) : void
        {
            this._titleBitmap.imgUrl = param1;
            return;
        }// end function

        public function set text(param1:String) : void
        {
            this._doubleCountDown.text = param1;
            return;
        }// end function

        public function resetCountDownPos(param1:int, param2:int) : void
        {
            this._doubleCountDown.x = param1;
            this._doubleCountDown.y = param2;
            return;
        }// end function

        public function stop() : void
        {
            this._doubleCountDown.stop();
            return;
        }// end function

        public function retsetTitlePos(param1:int, param2:int) : void
        {
            this._titleBitmap.x = param1;
            this._titleBitmap.y = param2;
            return;
        }// end function

    }
}
